﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Options;
using DevExpress.XtraGrid.Views.Grid;

namespace Wydatki
{
    public partial class MyBaseSqlCeGridForm : ProgNetComponentsDx.Forms.BaseSqlCeGridForm, IDataForm
    {
        public MyBaseSqlCeGridForm()
        {
            InitializeComponent();
            this.layoutProvider1.LayoutName = null;
            if (!this.IsDesignMode() && OptionCache.Grabber!=null)
            {
                OptionCache.OptionChanged += new OptionChangedEventHandler(OptionChanged);
                gvMain.SetFocusedRowStyle();
                gvChild.SetFocusedRowStyle();
            }
        }

        ~MyBaseSqlCeGridForm()
        {
            if (OptionCache.Grabber != null)
                OptionCache.OptionChanged -= new OptionChangedEventHandler(OptionChanged);
        }

        protected virtual void OptionChanged(object optionId)
        {
            if (OptionCache.IsTheSameId(optionId, Init.InitFormOptionNames.Wyglad.KolorZaznaczonegoWiersza) ||
                OptionCache.IsTheSameId(optionId, Init.InitFormOptionNames.Wyglad.KolorTekstuZaznaczonegoWiersza))
            {
                gvMain.SetFocusedRowStyle();
                gvChild.SetFocusedRowStyle();
            }
            if (Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll.IsTheSameKey(optionId))
                layoutProvider1.AutoSaveLayoutsOnFormClosing = OptionCache.GetOptionBoolValue(optionId);

            if (Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll.IsTheSameKey(optionId))
                layoutProvider1.AutoSaveLayoutsOnFormClosing = OptionCache.GetOptionBoolValue(optionId);
        }
        
        protected override void OnLoad()
        {
            if (!this.IsDesignMode())
            {
                SetLayoutMenuItemPos();
            }
            modeProvider1.CurrentMode = ProgNetComponentsDx.Enums.FormMode.None;
            base.OnLoad();
        }
        protected virtual void SetLayoutMenuItemPos()
        {
            bar1.SetItemPosition(bsiLayout, bar1.LinksPersistInfo.Count);
            this.layoutProvider1.AutoSaveLayoutsOnFormClosing = OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll);

        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.MdiFormClosing)
                e.Cancel = true;
            else
            {
                if (!Program.IsExit)
                {
                    FinishEdit();
                    if (changeDetectorProvider1.HasChanges)
                    {
                        DialogResult dlg = this.ShowDxYNCQuestionDialog("Chcesz zapisać zmiany?");
                        switch (dlg)
                        {
                            case DialogResult.Cancel:
                                e.Cancel = true;
                                break;
                            case DialogResult.No:
                                break;
                            case DialogResult.Yes:
                                if (ValidateAll())
                                {
                                    if (!Save())
                                    {
                                        this.ShowDxErrorDialog("Błąd zapisu!");
                                        e.Cancel = true;
                                    }
                                }
                                else
                                {
                                    this.ShowDxWarningDialog("Błąd validacji.");
                                    e.Cancel = true;
                                }
                                break;
                        }
                    }
                }
            }
            base.OnFormClosing(e);
        }

        #region IDataForm Members

        string IDataForm.Text
        {
            get
            {
                return this.Text;
            }
        }

        bool IDataForm.HasChanges
        {
            get { return this.changeDetectorProvider1.HasChanges; }
        }

        bool IDataForm.Save()
        {
            bool res =  this.Save();
            if (res)
            {
                changeDetectorProvider1.ResetAll();
                modeProvider1.CurrentMode = ProgNetComponentsDx.Enums.FormMode.Read;
            }
            return res;
        }

        void IDataForm.FinishEdit()
        {
            this.FinishEdit();
        }

        bool IDataForm.ValidateAll()
        {
            return ValidateAll();
        }

        #endregion
    }
}
